package com.example.tellwin.base;


import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.apkfuns.logutils.LogUtils;
import com.example.tellwin.AppHelper;
import com.example.tellwin.CallService;
import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.mine.act.LoginRegisterActivity;
import com.example.tellwin.utils.NetUtil;
import com.example.tellwin.utils.ToastUtil;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by wei.qt on 2018/4/13
 * 状态码和错误信息统一处理
 */

public class BaseObserver<T> implements Observer<BaseResponse<T>> {

    private SimpleCallback<T> simpleCallback;

    public BaseObserver(SimpleCallback simpleCallback) {
        this.simpleCallback = simpleCallback;
    }

    public BaseObserver() {

    }

    @Override
    public void onSubscribe(Disposable d) {
        if (simpleCallback != null) {
            simpleCallback.onLoading();
        }
       /* if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(MainApplication.getCurActivity());
        }
        loadingDialog.show();*/
    }

    @Override
    public void onNext(BaseResponse<T> tBaseEntity) {
       // LogUtils.e(tBaseEntity);
        if (tBaseEntity.isSuccess()) {
            //成功返回
            try {
                onSuccees(tBaseEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            //一般错误返回
            try {
                LogUtils.e(tBaseEntity);
                onCodeError(tBaseEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onError(Throwable e) {
        dismissDialog();
        e.printStackTrace();
        if (NetUtil.getNetWorkStatus(MainApplication.getContext()) == -1) {
            ToastUtil.show(MainApplication.getContext(), R.string.currently_no_network);
        } else if (e instanceof SocketTimeoutException) {
            ToastUtil.show(MainApplication.getContext(), R.string.timeout);
        } else if (e instanceof ConnectException) {
            ToastUtil.show(MainApplication.getContext(), R.string.network_interruption);
        } else {
            ToastUtil.show(MainApplication.getContext(), e.getMessage());
        }
        if (simpleCallback != null) {
            simpleCallback.onHttpError(e.getMessage());
        }
        if (simpleCallback != null) {
            simpleCallback.onComplete();
        }
    }

    @Override
    public void onComplete() {
        if (simpleCallback != null) {
            simpleCallback.onComplete();
        }
      dismissDialog();
    }

    private void dismissDialog(){
        /*try {
            if (loadingDialog != null) {
                loadingDialog.dismiss();
            }
        }catch (Exception e){
            e.printStackTrace();
        }*/
    }

    /**
     * 返回成功
     *
     * @param t
     * @throws Exception
     */
    protected void onSuccees(BaseResponse<T> t) throws Exception {
        dismissDialog();
        if (simpleCallback != null) {
            simpleCallback.onSuccess(t.content);
        }
    }

    /**
     * 返回成功了,但是code错误
     *
     * @param t
     * @throws Exception
     */
    protected void onCodeError(BaseResponse<T> t) throws Exception {
        dismissDialog();
        if (simpleCallback != null)
            simpleCallback.onFail(t.message);
        if(!TextUtils.isEmpty(t.message)) {
            if (t.message.equals("登录失效,请重新登录")) {
                AppHelper.getInstance().clearUser(MainApplication.getCurActivity());
                List<Activity> activityList = MainApplication.getActivityList();
                for (int i = 0; i < activityList.size(); i++) {
                    if (i == activityList.size() - 1) {
                        Intent intent = new Intent(activityList.get(i), LoginRegisterActivity.class);
                        activityList.get(i).startActivity(intent);
                    }
                    activityList.get(i).finish();
                }
                CallService.stop(MainApplication.getContext());
            }
            ToastUtil.show(MainApplication.getContext(), t.message);
        }else if(!TextUtils.isEmpty(t.msg)){
            ToastUtil.show(MainApplication.getContext(), "错误码:" +t.code + "-"+t.msg );
        }
    }

}
